package com.hzyl.famousreader.view.activity;

import android.arch.lifecycle.ViewModelProviders;
import android.content.res.ColorStateList;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.MenuItem;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.hzyl.famousreader.Constant;
import com.hzyl.famousreader.R;
import com.hzyl.famousreader.ThemeColor;
import com.hzyl.famousreader.router.Router;
import com.hzyl.famousreader.util.BottomNavigationViewHelper;
import com.hzyl.famousreader.view.fragment.*;
import com.hzyl.famousreader.viewmodel.MainViewModel;
import com.hzyl.famousreader.databinding.*;
/**
 * 主页
 */
@Route(path = Router.ACTIVITY_MAIN, name = "主页")
public class MainActivity extends BaseActivity {

  private ActivityMainBinding binding;

  private MainViewModel viewModel;

  @Autowired
  boolean changeTheme;

  //当前显示的Fragment
  private Fragment currentFragment = null;

  private BookshelfFragment bookshelfFragment;
  private ShopFragment shopFragment;
  private CategoryFragment categoryFragment;
  private DiscoveryFragment discoveryFragment;
  private UserFragment userFragment;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ARouter.getInstance().inject(this);

    binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

    int colorValue = SPUtils.getInstance().getInt(Constant.SP_THEME_COLOR,  ThemeColor.DEFAULT.getColor());
    int[][] states = new int[][]{{-16842912}, {16842912}};
    int[] colors = new int[]{-6513508, colorValue};
    ColorStateList colorStateList = new ColorStateList(states, colors);
    binding.navigation.setItemTextColor(colorStateList);
    binding.navigation.setItemIconTintList(colorStateList);

    BottomNavigationViewHelper.disableShiftMode(binding.navigation);

    binding.navigation.setOnNavigationItemSelectedListener((@NonNull MenuItem item) -> {
      Fragment fragment = null;
      switch (item.getItemId()) {
        case R.id.navigation_bookshelf:
          if (bookshelfFragment == null) {
            bookshelfFragment = (BookshelfFragment) ARouter.getInstance().build("/main/bookshelf").navigation();
          }
          fragment = bookshelfFragment;
          break;
        case R.id.navigation_shop:
          if (shopFragment == null) {
            shopFragment = (ShopFragment) ARouter.getInstance().build("/main/shop").navigation();
          }
          fragment = shopFragment;
          break;
        case R.id.navigation_category:
          if (categoryFragment == null) {
            categoryFragment = (CategoryFragment) ARouter.getInstance().build("/main/category").navigation();
          }
          fragment = categoryFragment;
          break;
        case R.id.navigation_discovery:
          if (discoveryFragment == null) {
            discoveryFragment = (DiscoveryFragment) ARouter.getInstance().build("/main/discovery").navigation();
          }
          fragment = discoveryFragment;
          break;
        case R.id.navigation_user:
          if (userFragment == null) {
            userFragment = (UserFragment) ARouter.getInstance().build("/main/user").navigation();
          }
          fragment = userFragment;
          break;
      }

      if (fragment == null) {
        return false;
      }

      switchFragment(fragment);
      return true;
    });

    viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
    viewModel.fragmentIndicator.observe(this, indicator -> binding.navigation.setSelectedItemId(indicator));

    viewModel.fragmentIndicator.setValue(R.id.navigation_bookshelf);
  }

  /**
   * 切换Fragment
   *
   * @param fragment 待显示的Fragment
   */
  private void switchFragment(Fragment fragment) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    if (!fragment.isAdded()) {
      transaction.add(R.id.fragment_wrapper, fragment);
    }
    if (currentFragment != null) {
      transaction.hide(currentFragment);
    }
    currentFragment = fragment;
    if(changeTheme) {
      LogUtils.d("修改了主题");
    }
    transaction.show(fragment);
    transaction.commit();
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    //如果用以下这种做法则不保存状态，再次进来的话会显示默认tab
    //总是执行这句代码来调用父类去保存视图层的状态
    //super.onSaveInstanceState(outState);
  }
}
